<?php
$loader->requireOnce('includes/LabQueue/LabQueueProcessor.class.php');


/**
 * Controler for processing lab result.
 * @package modules.dept_laboratory.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_LabQueue extends Controller {
	private $logger ;
	private $labQueueProcessor;
	function __construct($template_mod = "general") {
		parent::Controller();
		if (Celini::getCurrentController() == 'LabQueue') {
			$menu =& Menu::getInstance();
			$menu->currentSection = 'patient';
		}
		$this->logger = LoggerManager::getLogger('module.dept_laboratory.controllers.C_LabQueue');
		$this->template_mod = $template_mod;
		$this->view->path="lab_queue";
		$this->labQueueProcessor = new LabQueueProcessor($this);
	}
	/**
	 * Display Laboratory Queue UI
	 * @return html.
	 */
	public function actionRenderQueue() {
		$html = '';
		try {
			$patientType = Utils::getEnumArrayValue(EnumerationConstant::PATIENT_TYPE);
			$this->assign('patientType', $patientType);
			$html = $this->view->render('queue.html');
		} catch (EnumException $ex) {
			$this->logger->error('actionRenderQueue fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$this->logger->error('actionRenderQueue fail. Error detail: '. $ex->getMessage());
		}
		return $html;
	}
	/**
	 * render lab queue by patientType filter.
	 * @return lab queue ui.
	 */
	public function actionRenderQueueAjax() {
		$this->logger->info('Start actionRenderQueueAjax');
		$patientType = $_GET['patientType'];
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>array());
		try {
			$result['result']=$this->labQueueProcessor->renderQueue($patientType);

		} catch (EnumException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = CoreErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionRenderQueueAjax fail. Error detail: '. $ex->getMessage());
		}
		catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('actionRenderQueueAjax fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('actionRenderQueueAjax fail. Error detail: '. $ex->getMessage());

		}
		$this->logger->info('Finish actionRenderQueueAjax');
		return json_encode($result);
	}
	/**
	 * Add a patient into labqueue. Using by connector.
	 * @param $labAssignmentTicketId
	 * @return error code if got error.
	 */

	public function addPatient($labAssignmentTicketId) {
		$this->logger->info('Start addPatient has lab assignment [' . $labAssignmentTicketId . ']');
		$result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
		try {
			$result['result'] = $this->labQueueProcessor->addPatient($labAssignmentTicketId);

		} catch (LabException $ex) {
			$result['error']['code'] = $ex->getErrorCode();
			$result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
			$this->logger->error('addPatient fail. Error detail: '. $ex->getMessage());
		} catch (Exception $ex) {
			$result['error']['code'] = CoreErrorCode::COMMON_ERROR;
			$result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
			$this->logger->error('addPatient fail. Error detail: '. $ex->getMessage());
		}
		$this->logger->info('Finish addPatient has lab assignment [' . $labAssignmentTicketId . ']');
		return $result;
	}
	/**
	 * take an assignment into processing queue.
	 * @return unknown_type
	 */
	public function actionStartProcessQueueItemAjax() {
		$labAssignmentTicketId = $_GET['labAssignmentTicketId'];
		$this->logger->info('Start actionStartProcessQueueItem lab assignment [' . $labAssignmentTicketId . ']');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
        try {
            $result['result'] = $this->labQueueProcessor->startProcessQueueItem($labAssignmentTicketId);

        } catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionStartProcessQueueItem fail. Error detail: '. $ex->getMessage());
        } catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $this->logger->error('actionStartProcessQueueItem fail. Error detail: '. $ex->getMessage());
        }
        $this->logger->info('Finish actionStartProcessQueueItem lab assignment [' . $labAssignmentTicketId . ']');
        return json_encode($result);

	}
	/**
	 * Cancel an item in processing queue
	 * @return success if this item not input any result yet
	 * or this item not done yet.
	 */
    public function actionCancelProcessingQueueItemAjax() {
        $labAssignmentTicketId = $_GET['labAssignmentTicketId'];
        $this->logger->info('Start actionCancelProcessingQueueItem lab assignment [' . $labAssignmentTicketId . ']');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
        try {
            $result['result'] = $this->labQueueProcessor->cancelProcessingQueueItem($labAssignmentTicketId);

        } catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionCancelProcessingQueueItem fail. Error detail: '. $ex->getMessage());
        } catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $this->logger->error('actionCancelProcessingQueueItem fail. Error detail: '. $ex->getMessage());
        }
        $this->logger->info('Finish actionCancelProcessingQueueItem lab assignment [' . $labAssignmentTicketId . ']');
        return json_encode($result);

    }

/**
     * Finish an item in processing queue
     * @return success if this item not input any result yet
     * or this item not done yet.
     */
    public function actionFinishProcessingQueueItemAjax($labAssignmentTicketId = 0) {
        if (isset($_GET['labAssignmentTicketId'])) {
            $labAssignmentTicketId = $_GET['labAssignmentTicketId'];
        }
        $this->logger->info('Start actionFinishProcessingQueueItemAjax lab assignment [' . $labAssignmentTicketId . ']');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
        try {
            $result['result'] = $this->labQueueProcessor->finishProcessingQueueItem($labAssignmentTicketId);

        } catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionFinishProcessingQueueItemAjax fail. Error detail: '. $ex->getMessage());
        } catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $this->logger->error('actionFinishProcessingQueueItemAjax fail. Error detail: '. $ex->getMessage());
        }
        $this->logger->info('Finish actionFinishProcessingQueueItemAjax lab assignment [' . $labAssignmentTicketId . ']');
        return json_encode($result);

    }

/**
     * Finish an item in processing queue
     * @return success if this item not input any result yet
     * or this item not done yet.
     */
    public function actionPreFinishProcessingQueueItemAjax() {
        $labAssignmentTicketId = $_GET['labAssignmentTicketId'];
        $this->logger->info('Start actionPreFinishProcessingQueueItemAjax lab assignment [' . $labAssignmentTicketId . ']');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
        try {
            $result['result'] = $this->labQueueProcessor->preFinishProcessingQueueItem($labAssignmentTicketId);

        } catch (LabException $ex) {
            $result['error']['code'] = $ex->getErrorCode();
            $result['error']['mess'] = LabErrorCode::getErrorMess($ex->getErrorCode());
            $this->logger->error('actionPreFinishProcessingQueueItemAjax fail. Error detail: '. $ex->getMessage());
        } catch (Exception $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $this->logger->error('actionPreFinishProcessingQueueItemAjax fail. Error detail: '. $ex->getMessage());
        }
        $this->logger->info('Finish actionPreFinishProcessingQueueItemAjax lab assignment [' . $labAssignmentTicketId . ']');
        return json_encode($result);

    }

}
?>